package com.cloudlead.shipownerAction.action;

import com.cloudlead.common.lang.annotation.Label;
import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.common.worker.action.impl.AbstractActionFactory;
import com.cloudlead.common.worker.annotation.Action;
import com.cloudlead.common.worker.annotation.Order;
import com.cloudlead.commonData.service.*;
import com.cloudlead.commonData.vo.BaseCompany;
import com.cloudlead.commonData.vo.TbContainerInfo;
import com.cloudlead.commonData.vo.TbFleetOrder;
import com.cloudlead.shipownerAction.BusinessModules;
import com.cloudlead.shipownerAction.service.CustomerOrderSer;
import com.cloudlead.shipownerService.CustomerOrderService;
import com.cloudlead.shipownerService.vo.CustomerOrderVo;
import com.cloudlead.shipownerService.vo.TbContainerInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2015/11/9.
 */
@Controller
public class CustomerOrderActionFactory extends AbstractActionFactory implements CustomerOrderService {
    public CustomerOrderActionFactory() {super(BusinessModules.BookSpaceOrderManagement);}
    @Autowired
    CustomerOrderSer bookingSpaceOrderSer;
    @Autowired
    TbFleetOrderService fleetOrderSer;
    @Autowired
    BaseVoyageService baseVoyageSer;
    @Autowired
    BaseShipRouteService baseShipRouteSer;
    @Autowired
    TbContainerInfoService tbContainerInfoService;
    @Autowired
    BaseCompanyService baseCompanyService;
    @Autowired
    CustomerOrderSer customerOrderSer;

    @Override
    @Action
    @Order(1)
    @Label("船东订舱列表查询")
    public EasyuiGridData orderList(CustomerOrderVo vo,@Label("页码")@NotNull int page,@Label("条数")@NotNull int rows) {
        EasyuiGridData rltData=new EasyuiGridData(bookingSpaceOrderSer.orderList(vo, page, rows),
                "id,bussinessCode,commodity,polName,delName,ordertime,belongCompany," +
                        "polOrderState,delOrderState," +
                        "baseCompany.id baseCompanyId," +
                        "baseVoya.vessel vessel," +
                        "baseVoya.voyage voyage," +
                        "baseVoya.state voyageState," +
                        "baseVoya.startExpectedTime startExpectedTime," +
                        "baseVoya.startTime startTime," +
                        "baseVoya.arriveExpectedTime arriveExpectedTime," +
                        "baseVoya.arriveTime arriveTime"
                        );
        for (Object o:rltData.getRows()){
            HashMap map=(HashMap)o;
            String coId=(String)map.get("id");
            List<Term> tr=new ArrayList<Term>();
            tr.add(new Term("tbCustomerOrder.id", coId));
            Filter filter = new Filter(new Criteria( tr));
            List<TbFleetOrder> foList=fleetOrderSer.list(filter,null);
            String fleetCmpName="";
            for (TbFleetOrder fo:foList){
                if (fleetCmpName.indexOf(fo.getFleetCompany().getCompanyName())<0)
                    fleetCmpName+="/"+fo.getFleetCompany().getCompanyName();
            }
            if (!fleetCmpName.equals("")) fleetCmpName=fleetCmpName.substring(1);
            map.put("fleetCompanyName",fleetCmpName);
//            String fleetStaus="未派车";
//            int completeCount=0;
//            List<TbContainerInfo> ctnList=tbContainerInfoService.list(filter,null);
//            for (TbContainerInfo ctn:ctnList){
//                if (ctn.getFleetStatus()!=null && !ctn.getFleetStatus().equals("未派车")) fleetStaus="已执行";
//                if (ctn.getFleetStatus()!=null && ctn.getFleetStatus().equals("已完成")) completeCount++;
//            }
//            if (completeCount>0 && completeCount==ctnList.size()) fleetStaus="已完成";
//            map.put("fleetStatus",fleetStaus);
            map.put("isStart", vo.getBaseCompany().equals(map.get("baseCompanyId")) ? "1" : "0");
        }
        return rltData;
    }


    @Override
    @Action
    @Order(2)
    @Label("查询箱信息")
    public EasyuiGridData containerList(TbContainerInfoVo vo,@Label("页码")@NotNull int page,@Label("条数")@NotNull int rows) {
        Page<TbContainerInfo> bkOrders=bookingSpaceOrderSer.containerList(vo, page, rows);
        for (TbContainerInfo bkOrd:bkOrders.getContent()){
            TbFleetOrder fleetOrder=bkOrd.getFleetOrder();
            if(fleetOrder!=null && fleetOrder.getFleetCompany()!=null){
                BaseCompany baseCompany=baseCompanyService.view(fleetOrder.getFleetCompany().getId());
                fleetOrder.setFleetCompany(baseCompany);
            }
        }
        return new EasyuiGridData(bkOrders,"id," +
                "containerNo,type," +
                "fleetStatus,carNumber,dangerous," +
                "fleetOrder.fleetCompany.companyName fleetName," +
                "fleetOrder.packagingAddress packagingAddress," +
                "fleetOrder.removeAddress removeAddress," +
                "fleetOrder.cargoName cargoName," +
                "fleetOrder.storeArea storeArea," +
                "sendCarPepole,saveDate,sendCarTime,minato," +
                "temp,contactPerson,contactTel,car.licencePlate licencePlate," +
                "trailerCar.licencePlate trailerLicencePlate,suitcaseTime," +
                "intoTime,outTime,finishTime,feedBack");
    }

    @Override
    @Action
    @Order(3)
    @Label("所属公司列表")
    public ObjectList companyList(@Label("公司ID")@NotNull String companyId) {
        return new ObjectList(customerOrderSer.companyList(companyId),"id,belongCompany");
    }
}
